//
//  YZEYijianfankuiViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/3/29.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZEYijianfankuiViewController.h"
#import <QMUIKit/QMUIKit.h>
#import "YYTextView.h"

#define MAX_LIMIT_NUMS     500     //来限制最大输入只能100个字符
#define URL_givefeedback @"/api/feedbacks" //反馈提交接口

@interface YZEYijianfankuiViewController ()<YYTextViewDelegate>
@property (weak, nonatomic) IBOutlet YYTextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *count_Lable;
@end

@implementation YZEYijianfankuiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"意见反馈";
    self.automaticallyAdjustsScrollViewInsets=NO;
    UIBarButtonItem *btn_tj = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    
    self.navigationItem.rightBarButtonItem = btn_tj;
    self.count_Lable.text = @"0/500";
}

-(void)save{
    if ([self.textView isNotEmpty]) {
        [[YZENetwork sharedInstace]postRequestWithUrl:[BASE stringByAppendingString:URL_givefeedback]
                                           parameters:@{@"name":self.textView.text}
                                             delegate:self
                                           requestTag:1
                                         successBlock:^(id responseObject)
        {
            [UIView showToastWindowWithMessage:@"已收到您的反馈"];
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
    }    
}

#pragma YYTextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text
{
    NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    NSInteger caninputlen = MAX_LIMIT_NUMS - comcatstr.length;
    
    if (caninputlen >= 0)
    {
        return YES;
    }
    else
    {
        NSInteger len = text.length + caninputlen;
        //防止当text.length + caninputlen < 0时，使得rg.length为一个非法最大正数出错
        NSRange rg = {0,MAX(len,0)};
        
        if (rg.length > 0)
        {
            NSString *s = [text substringWithRange:rg];
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
        }
        return NO;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (existTextNum > MAX_LIMIT_NUMS)
    {
        //截取到最大位置的字符
        NSString *s = [nsTextContent substringToIndex:MAX_LIMIT_NUMS];
        
        [textView setText:s];
    }
    //不让显示负数
    self.count_Lable.text = [NSString stringWithFormat:@"%ld/%d", existTextNum, MAX_LIMIT_NUMS];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
